// pages/OrderDetail/OrderDetail.js
import {
  queryOrderDetail,
  cancelOrder,
  deleteOrder,
  updateOrderDeliveryAddress
} from "../../service/order"
import {
  orderStatus
} from "../../constant/OrderConstant"
import dayjs from 'dayjs';
import {
  queryDeliveryAddressById,
} from "../../service/delivery-address"
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 胶囊距离屏幕顶部的距离
    top: app.globalData.menuButtonTop,
    // 胶囊高度
    height: app.globalData.menuButtonHeight,
    // 胶囊左方大小
    left: app.globalData.menuButtonLeft,
    // 胶囊底部距离屏幕顶部的距离
    bottom: app.globalData.menuButtonBottom,
    // 收货人
    receiverName: "",
    // 收货手机号
    receiverPhone: "",
    // 收货地址
    receiverDeliveryAddress: "",
    // 商品列表
    productList: [],
    // 订单总价
    price: 0.0,
    // 订单编号
    orderNumber: "",
    // 订单id
    orderId: -1,
    // 下单时间
    orderTime: null,
    // 距离订单关闭还有多久（单位-毫秒）
    remainingTime: 86400000,
    remainingTimeData: {},
    // 订单备注
    note: "",
    // 订单状态
    status: -1,
    // 订单状态枚举类
    orderStatus: orderStatus
  },
  /**
   * 点击复制订单号
   */
  onTapOrderNumber() {
    wx.setClipboardData({
      data: this.data.orderNumber,
      success(res) {
        // 无视此次的警告
        wx.hideLoading();
        wx.showToast({
          title: '复制成功',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  /**
   * 点击前往商品页
   */
  onTapNavigateToProductPage(event) {
    const productId = event.currentTarget.dataset.productId
    console.log(productId);
    wx.navigateTo({
      url: `/pages/Product/Product?productId=${productId}`,
    })
  },
  /**
   * 点击修改地址
   */
  onTapModifyDeliveryAddress() {
    console.log("修改地址");
    const self = this
    wx.navigateTo({
      url: '/pages/DeliveryAddress/DeliveryAddress',
      events: {
        // 接受到地址页面发送的地址Id
        async selectDeliveryAddress(addressId) {
          console.log(addressId);
          wx.showLoading({
            title: '加载中',
          })
          const res = await queryDeliveryAddressById(addressId)
          // 修改订单的地址
          await updateOrderDeliveryAddress(addressId, self.data.orderId)
          // 更新页面
          self.setData({
            receiverName: res.data.receiverName,
            receiverPhone: res.data.receiverPhone,
            receiverDeliveryAddress: res.data.province + res.data.city + res.data.district + res.data.detailAddress
          })
          wx.hideLoading()
        }
      }
    })
  },
  /**
   * 点击取消订单
   */
  onTapCancelOrder() {
    const self = this
    wx.showModal({
      title: '提示',
      content: '确认取消订单？',
      complete: async (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
            mask: true
          })
          await cancelOrder(self.data.orderId)
          wx.hideLoading()
          await this.onLoad({
            orderId: self.data.orderId
          })
        }
      }
    })
  },
  /**
   * 点击联系客服
   */
  onTapContactSupport() {
    console.log("点击联系客服");
  },
  /**
   * 立即购买
   */
  onTapBuyNow() {
    console.log("点击立即购买");
  },
  // 倒计时函数
  onTimeChange(event) {
    this.setData({
      remainingTimeData: event.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    const orderId = Number(options.orderId)
    console.log(orderId);
    const {
      data
    } = await queryOrderDetail(orderId)
    this.setData({
      orderId: data.orderId,
      orderNumber: data.orderNumber,
      orderTime: data.orderTime,
      price: data.price,
      receiverDeliveryAddress: data.receiverAddress,
      receiverName: data.receiverName,
      receiverPhone: data.receiverPhone,
      status: data.status,
      productList: data.orderList,
      note: data.orderNote
    })
    const originalTime = this.data.orderTime
    const targetTime = dayjs(originalTime).add(3, 'day');
    const now = dayjs();
    const millisecondsDiff = targetTime.diff(now, 'millisecond');
    this.setData({
      remainingTime: millisecondsDiff
    })
    console.log(this.data);
    wx.hideLoading()
  },
  /**
   * 点击返回到上一页
   */
  onTapNavigateBack() {
    wx.navigateBack()
  },
  /**
   * 删除订单
   */
  onTapDeleteOrder() {

  },
  /**
   * 加入购物车
   */
  onTapAddToShippingCart() {

  },
  /**
   * 立即购买
   */
  onTapRepeatPurchase() {

  },
  /**
   *  取消订单
   */
  onTapCancelOrder() {

  },
  /**
   * 立即支付
   */
  onTapBuyNow() {

  },
  /**
   * 申请开票 
   */
  onTapApplyForInvoice() {

  },
  /**
   * 催发货
   */
  onTapRemindShipMent() {

  },
  /**
   * 查看物流
   */
  onTapTrackShipment() {

  },
  /**
   * 确认收货
   */
  onTapConfirmDelivery() {

  },
  /**
   * 立即评价
   */
  onTapTateNow() {

  }
})